import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class CartServlet extends HttpServlet {
    private static final Logger logger = Logger.getLogger(CartServlet.class.getName());

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String userId = (String) request.getSession().getAttribute("user_id"); // 从 session 中获取用户 ID

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<CartItem> carList = new ArrayList<>();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/shop?serverTimezone=Asia/Shanghai&useSSL=false";
            conn = DriverManager.getConnection(url, "root", "123456");

            String sql = "SELECT goods_id, goods_name, unitprice, quantity, pic FROM car WHERE userid = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, userId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                CartItem item = new CartItem();
                item.setGoodsId(rs.getString("goods_id"));
                item.setGoodsName(rs.getString("goods_name"));
                item.setUnitPrice(rs.getString("unitprice"));
                item.setQuantity(rs.getInt("quantity"));
                item.setPic(rs.getString("pic"));
                carList.add(item);
            }
            // 将 carList 存储到 session 作用域
            request.getSession().setAttribute("carList", carList);


            // 转发请求到 car.jsp 页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("car.jsp");
            dispatcher.forward(request, response);
        } catch (Exception e) {
            logger.severe("Error fetching cart items: " + e.getMessage());
            e.printStackTrace();

        } finally {
            // 关闭资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    logger.severe("Error closing ResultSet: " + e.getMessage());
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    logger.severe("Error closing PreparedStatement: " + e.getMessage());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    logger.severe("Error closing Connection: " + e.getMessage());
                }
            }
        }
    }

    public class CartItem {
        private String goodsId;
        private String goodsName;
        private String unitPrice;
        private int quantity;
        private String pic;

        // Getters and Setters
        public String getGoodsId() {
            return goodsId;
        }

        public void setGoodsId(String goodsId) {
            this.goodsId = goodsId;
        }

        public String getGoodsName() {
            return goodsName;
        }

        public void setGoodsName(String goodsName) {
            this.goodsName = goodsName;
        }

        public String getUnitPrice() {
            return unitPrice;
        }

        public void setUnitPrice(String unitPrice) {
            this.unitPrice = unitPrice;
        }

        public int getQuantity() {
            return quantity;
        }

        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }
    }
}